﻿#region

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Net.Mail;
using System.Windows.Forms;
using MailQ.API;
using MailQ.Test.Properties;

#endregion

namespace MailQ.Test
{
	public partial class MailQTest : Form
	{
		public MailQTest()
		{
			InitializeComponent();
		}

		private void Send_Click(object Sender, EventArgs E)
		{
			Send.Enabled = false;

			Dictionary<int, MailMessage> FailedMessages = new Dictionary<int, MailMessage>(0);
			Dictionary<int, MailMessage> SentMessages = new Dictionary<int, MailMessage>(0);

			foreach (KeyValuePair<int, MailMessage> MailMessage in EmailHelper.GetEmailsToSend(Settings.Default.SendingWait, Settings.Default.FailedWait))
			{
				MailQException Ex = new MailQException();
				if (EmailHelper.Send(MailMessage.Value, ref Ex, MailMessage.Key, GetSetting(Setting.UserName),
									GetSetting(Setting.Password), GetSetting(Setting.SmtpServer),
									int.Parse(GetSetting(Setting.SmtpPort)),
									bool.Parse(GetSetting(Setting.UseDefaultCredentials)),
									bool.Parse(GetSetting(Setting.UseSsl)), GetSetting(Setting.BccAddress),
									GetSetting(Setting.TrackingMessage)))
				{
					SentMessages.Add(MailMessage.Key, MailMessage.Value);
					MessageBox.Show(string.Format("Message {0} has been sent to {1}", MailMessage.Key, MailMessage.Value.To));
					EmailHelper.AddToLog(
						string.Format("Message {0} has been sent to {1}", MailMessage.Key, MailMessage.Value.To), MailMessage.Key);
				}
				else
				{
					FailedMessages.Add(MailMessage.Key, MailMessage.Value);
					MessageBox.Show(string.Format("Message {0} failed. Would've sent to {1}. Reason: {2} {3}",
												  MailMessage.Key, MailMessage.Value.To, Ex.Message,
												  Ex.InnerException != null
													? string.Format("-- {0}", Ex.InnerException.Message)
													: string.Empty));
					EmailHelper.AddToLog(string.Format("Message {0} failed. Would've sent to {1}. Reason: {2} {3}",
													   MailMessage.Key, MailMessage.Value.To, Ex.Message,
													   Ex.InnerException != null
														? string.Format("-- {0}", Ex.InnerException.Message)
														: string.Empty), MailMessage.Key);
				}
			}

			EmailHelper.UpdateFailedMessages(FailedMessages);
			EmailHelper.UpdateSentMessages(SentMessages);
			Send.Enabled = true;
		}

		private string GetSetting(Setting Setting)
		{
			string KeyWord;
			switch (Setting)
			{
				case Setting.UserName:
					KeyWord = "UserName";
					break;
				case Setting.Password:
					KeyWord = "Password";
					break;
				case Setting.SmtpServer:
					KeyWord = "SmtpServer";
					break;
				case Setting.SmtpPort:
					KeyWord = "SmtpPort";
					break;
				case Setting.UseSsl:
					KeyWord = "UseSSL";
					break;
				case Setting.UseDefaultCredentials:
					KeyWord = "UseDefaulCredentials";
					break;
				case Setting.TrackingMessage:
					KeyWord = "TrackingMessage";
					break;
				case Setting.FromAddress:
					KeyWord = "FromAddress";
					break;
				case Setting.BccAddress:
					KeyWord = "BccAddress";
					break;
				default:
					throw new ArgumentOutOfRangeException("Setting");
			}
			return ConfigurationManager.AppSettings[KeyWord];
		}

		private void IsHtml_CheckedChanged(object Sender, EventArgs E)
		{
			Message.Text = IsHtml.Checked
							? "This is a <b>test</b> message generated by the <i>test</i> application."
							: "This is a test message generated by the test application.";
		}

		private void Queue_Click(object Sender, EventArgs E)
		{
			Queue.Enabled = false;
			MailMessage Msg = new MailMessage
							  {
								  Subject = Subject.Text,
								  IsBodyHtml = IsHtml.Checked,
								  Body = Message.Text,
								  From = new MailAddress(From.Text)
							  };
			foreach (string Str in To.Text.Split(','))
			{
				Msg.To.Add(new MailAddress(Str));
			}
			foreach (string Str in CC.Text.Split(','))
			{
				Msg.CC.Add(new MailAddress(Str));
			}
			foreach (string Str in BCC.Text.Split(','))
			{
				Msg.Bcc.Add(new MailAddress(Str));
			}
			MailQException Ex = new MailQException();
			MessageBox.Show(EmailHelper.AddEmailToQueue(Msg, ref Ex) ? "Email added to queue!" : string.Format("Couldn't add email to queue! Reason: {0}", Ex.Message));

			Queue.Enabled = true;
		}

		#region Nested type: Setting

		private enum Setting
		{
			UserName,
			Password,
			SmtpServer,
			SmtpPort,
			UseSsl,
			UseDefaultCredentials,
			TrackingMessage,
			FromAddress,
			BccAddress
		}

		#endregion
	}
}